﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Common;
using Edge.Common.Services;
using Edge.Common.Web;
using Edge.Common.Web.Navigation;

namespace Shop.Web.Controls
{
    public partial class Pager : System.Web.UI.UserControl
    {
        private int _count = 0;
        private FetchOptionForm _options;

        public int Count
        {
            get { return _count; }
            set { _count = value; }
        }

        public FetchOptionForm Options
        {
            get { return _options; }
            set { _options = value; }
        }

        public int SetPager()
        {
            int pages = 0;
            if (_count != 0 && _options != null)
            {
                if (_count <= _options.Rows)
                {
                    Visible = false;
                }
                else
                {
                    Visible = true;
                    pages = Convert.ToInt32(Math.Ceiling((Convert.ToDecimal(_count) / _options.Rows)));
                    string Url = Locator.GetService<INavigationService>().GetCurrentQueryString(new UrlBuilder(Request.Url.AbsoluteUri), new string[] { "page" });
                    if (Url.IndexOf("?") > 0)
                    {
                        lnkAll.HRef = Url + (Url.IndexOf("?") == Url.Length ? "rows=" : "&rows=") +(_count+1).ToString();
                        Url += Url.IndexOf("?") == Url.Length ? "page=" : "&page=";
                    }
                    else
                    {
                        lnkAll.HRef = Url + "?rows=" + (_count+1).ToString();
                        Url += "?page=";
                    }
                    if (_options.Page > 1)
                    {
                        lnkPrevious.Visible = true;
                        lnkPrevious.HRef = Url + (_options.Page - 1).ToString();
                    }
                    if (_options.Page == pages)
                        lnkNext.Visible = false;
                    else
                        lnkNext.HRef = Url + (_options.Page + 1);

                    int min = _options.Page;
                    int max = _options.Page;

                    while (min > 1 && min > _options.Page - 4)
                        min--;
                    while (max < pages && (max < _options.Page + 4 || max - min < 8))
                        max++;

                    IList p = new ArrayList();

                    for (int i = min; i <= max; i++)
                    {
                        string s = "<span class='current'>" + i + "</span>";
                        if (i != _options.Page)
                            s = "<a href=\""+Url + i.ToString()+"\" >" + i + "</a>";
                        p.Add(s);
                    }
                    int start = _options.Page*_options.Rows - _options.Rows + 1;
                    int finish = _options.Page*_options.Rows < _count ? _options.Page*_options.Rows : _count;
                    ltRange.Text = start.ToString() + "-" + finish.ToString();
                    ltCount.Text = _count.ToString();
                    
                    rpPager.DataSource = p;
                    rpPager.DataBind();
                }
            }
            return pages;
        }

        public int SetPager(int count, FetchOptionForm options)
        {
            if (count > 0)
            {
                _count = count;
                _options = options;
                return SetPager();
            }
            phNoResult.Visible = true;
            phResult.Visible = false;
            return 0;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}